package priv.happy.mealpickpro.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import priv.happy.mealpickpro.domain.UserMeal;
import priv.happy.mealpickpro.dto.UserDTO;
import priv.happy.mealpickpro.mapper.AuthenticationMapper;
import priv.happy.mealpickpro.service.AuthenticationService;
import priv.happy.mealpickpro.service.UserMealService;
import priv.happy.mealpickpro.util.ImgUtil;
import priv.happy.mealpickpro.util.JWTUtil;
import priv.happy.mealpickpro.vo.UserVO;

import java.time.Duration;

/**
 * @Author: HAPPY
 * @Project_name: MealPick
 * @Package_name: priv.happy.mealpick.service.impl
 * @Date: 2023/7/2 11:57
 * @Description:
 */
@Service
public class AuthenticationServiceImpl implements AuthenticationService {
    @Autowired
    AuthenticationMapper authenticationMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    UserMealService userMealService;

    @Override
    public UserVO login(UserMeal loginUser) {
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        UserVO userVO = new UserVO();
        // 密码加密
        loginUser.setPassword(SecureUtil.sha256(loginUser.getPassword()));
        // 登录
        loginUser = userMealService.login(loginUser);
        // 登录结果判断
        if (loginUser != null && loginUser.getId() != null) {
            // 利用用户邮箱生成token
            String token = JWTUtil.getToken(loginUser.getId(), loginUser.getEmail(), loginUser.getRole());
            // token默认有效期为1周
            operations.set("mealpickpro:token:" + loginUser.getEmail(), token, Duration.ofDays(7));
            userVO.setToken(token).setUserMeal(loginUser);
        }
        return userVO;
    }

    @Override
    public boolean isRegistered(String email) {
        Integer result = authenticationMapper.isExist(email);
        return result == 1;
    }

    @Override
    public int register(UserDTO user) {
        if (user == null || user.getEmail() == null || user.getPassword() == null || user.getCaptcha() == null) {
            return 2;
        } else {
            // 查询该邮箱是否已经注册
            if (!isRegistered(user.getEmail())) {
                // 查询验证码
                String captcha = stringRedisTemplate.opsForValue().get("mealpickpro:captcha:register:" + user.getEmail());
                if (captcha != null && captcha.equals(user.getCaptcha())) {
                    UserMeal userMeal = new UserMeal()
                            .setName("无敌暴龙战神66")
                            .setPassword(SecureUtil.sha256(user.getPassword()))
                            .setEmail(user.getEmail())
                            .setAvatar(ImgUtil.imageShowAvatarFolder + "/default-avatar.png")
                            .setRole("普通用户")
                            .setRegisterTime(DateUtil.now());
                    try {
                        userMealService.save(userMeal);
                        return 0;
                    } catch (Exception e) {
                        return 4;
                    }
                } else {
                    return 3;
                }
            } else {
                return 1;
            }
        }
    }
}
